package com.example.settings;

import android.content.Context;

import com.example.storage.Storage;

public class Setting { 
	
	private Erode erode;
	private Dilate dilate;
	private CercleSound cercleSound;
	private Homography homography;
	private VideoSettings videoSettings;
	private Hsv hsv;
	private int[] array;
	private int change;
	
	public Setting(Erode erode, Dilate dilate, CercleSound cercleSound, Homography homography, VideoSettings videoSettings, Hsv hsv){
		this.erode=erode;
		this.dilate=dilate;
		this.cercleSound=cercleSound;
		this.homography=homography;
		this.videoSettings=videoSettings;
		this.hsv=hsv;
		array = new int[16];

	}
	
	public Setting() {
		array = new int[16];

	}

	public Erode getErodeSettings() {
		return erode;
	}
	
	public Dilate getDilateSettings() {
		return dilate;
	}
	
	public void setDilateSettings(Dilate dilateSettings) {
		this.dilate = dilateSettings;
	}
	
	public void setErodeSettings(Erode erodeSettings) {
		this.erode = erodeSettings;
	}
	
	public CercleSound getCercleSound() {
		return cercleSound;
	}

	public void setCercleSound(CercleSound cercleSound) {
		this.cercleSound = cercleSound;
	}
	
	public Homography getHomography() {
		return homography;
	}

	public void setHomography(Homography homography) {
		this.homography = homography;
	}

	public VideoSettings getVideoSettings() {
		return videoSettings;
	}

	public void setVideoSettings(VideoSettings videoSettings) {
		this.videoSettings = videoSettings;
	}

	public Hsv getHsv() {
		return hsv;
	}

	public void setHsv(Hsv hsv) {
		this.hsv = hsv;
	}

	public int[] getArray() {
		return array;
	}
	
	public int getChange() {
		return change;
	}

	public void setChange(int change) {
		this.change = change;
	}
	
	/***
	 * update the array when we load the array from the file
	 * @param array, the array loaded from the file
	 */
	public void setArray(int [] array) {
		
		boolean complete, partial, no, fisheye, videoDisplay;
		
		this.homography = new Homography(array[0],array[1]);
		this.hsv = new Hsv(array[2],array[3],array[4],array[5]);
		
		if(array[6] == 1){
			this.dilate = new Dilate(array[7], true);
		}else {
			this.dilate = new Dilate(array[7], false);
		}
		if(array[8] == 1){
			this.erode = new Erode(array[9],true);
		}else {
			this.erode = new Erode(array[9],false);
		}
		if(array[10] == 1){
			complete=true;
			partial=false;
			no=false;
		}else if(array[10] == 2){
			complete=false;
			partial=true;
			no=false;
		}else{
			complete=false;
			partial=false;
			no=true;
		}
		if(array[11] == 1){
			fisheye=true;
		}else {
			fisheye=false;
		}
		if(array[12] == 1){
			videoDisplay=true;
		}else {
			videoDisplay=false;
		}
		this.videoSettings = new VideoSettings(complete, partial, no, fisheye, videoDisplay);
		this.cercleSound = new CercleSound(array[13],array[14]);
		this.change = array[15];
		this.array = array;
	}

	/***
	 * transform the object in a array in order to update the video settings
	 * @param context, the context where the function is called
	 */
	public void saveArray(Context context) {	
		array[0] = this.homography.getDeformation();
		array[1] = this.homography.getZoom();
		array[2] = this.hsv.getMin1();
		array[3] = this.hsv.getMin2();
		array[4] = this.hsv.getMax1();
		array[5] = this.hsv.getMax2();
		array[6] = (this.dilate.isUsed()) ? 1 : 0;
		array[7] = this.dilate.getNbIteration();
		array[8] = (this.erode.isUsed()) ? 1 : 0;
		array[9] = this.erode.getNbIteration();
		if(this.videoSettings.isCompleteTreatment())
			array[10] = 1;
		else if(this.videoSettings.isPartialTreatment())
			array[10] = 2;
		else if(this.videoSettings.isNoTreatment())
			array[10] = 3;
		array[11] = (this.videoSettings.isFisheye()) ? 1 : 0;
		array[12] = (this.videoSettings.isVideoDisplay()) ? 1 : 0;
		array[13] = this.cercleSound.getCercle();
		array[14] = this.cercleSound.getSound();
		array[15] = this.change;
		
		Storage.save(context, array);
	}
	
	/***
	 * reset the settings
	 * @param context, the context where the function is called
	 */
	public void resetSettings(Context context){
		
		this.erode.setNbIteration(1);
		this.erode.setUsed(true);
		this.erode.setRefresh(true);
		
		this.dilate.setNbIteration(3);
		this.dilate.setUsed(true);
		this.dilate.setRefresh(true);
		
		this.cercleSound.setCercle(10);
		this.cercleSound.setSound(4);
		
		this.homography.setDeformation(7715);
		this.homography.setZoom(26731);
		this.homography.setRefresh(true);
		
		this.videoSettings.setCompleteTreatment(true);
		this.videoSettings.setPartialTreatment(false);
		this.videoSettings.setNoTreatment(false);
		this.videoSettings.setFisheye(false);
		this.videoSettings.setVideoDisplay(true);
		this.videoSettings.setRefresh(true);
		
		this.hsv.setMin1(0);
		this.hsv.setMax1(8);
		this.hsv.setMin2(175);
		this.hsv.setMax2(179);
		this.hsv.setRefresh(true);
		
		saveArray(context);
		
	}

	
	
}
